from datetime import datetime
from typing import Optional
from fastapi import APIRouter
from apps.schemas.schemas import UserCreateModel, UserUpdateModel
from apps.services.user import (
    get_all_users,
    get_user_by_id,
    get_user_details_by_id,
    create_user,
    update_user,
    delete_user
)

users = APIRouter()


@users.get("/msg")
async def get_all_users_api():
    """
    获取所有用户列表
    """
    try:
        users_data = await get_all_users()
        return {
            "status": 200,
            "message": "success",
            "data": users_data
        }
    except Exception as e:
        return {
            "status": 500,
            "message": f"Error retrieving users: {str(e)}",
            "data": []
        }


@users.get("/secret/msg/{user_id}")
async def get_user_details_by_id_api(user_id: int):
    """
    根据用户ID获取用户详细信息（包含敏感信息）
    """
    try:
        user_data = await get_user_details_by_id(user_id)
        if user_data:
            return {
                "status": 200,
                "message": "success",
                "data": [user_data]  # 保持与原API相同的返回格式
            }
        else:
            return {
                "status": 404,
                "message": "Not Found",
                "data": "没有此用户"
            }
    except Exception as e:
        return {
            "status": 500,
            "message": f"Error retrieving user details: {str(e)}",
            "data": "没有此用户"
        }


@users.get("/msg/{user_id}")
async def get_user_by_id_api(user_id: int):
    """
    根据用户ID获取用户信息
    """
    try:
        user_data = await get_user_by_id(user_id)
        if user_data:
            return {
                "status": 200,
                "message": "success",
                "data": [user_data]  # 保持与原API相同的返回格式
            }
        else:
            return {
                "status": 404,
                "message": "Not Found",
                "data": "没有此用户"
            }
    except Exception as e:
        return {
            "status": 500,
            "message": f"Error retrieving user: {str(e)}",
            "data": "没有此用户"
        }


@users.post("/add")
async def create_user_api(data: UserCreateModel):
    """
    创建新用户
    """
    try:
        user_data = await create_user(data)
        return {
            "status": 201,
            "message": "success",
            "data": "成功添加用户信息"
        }
    except Exception as e:
        return {
            "status": 500,
            "message": f"Error creating user: {str(e)}",
            "data": "创建用户失败"
        }


@users.put("/update/{user_id}")
async def update_user_api(user_id: int, data: UserUpdateModel):
    """
    更新用户信息
    """
    try:
        updated = await update_user(user_id, data)
        if updated:
            return {
                "status": 200,
                "message": "success",
                "data": "更新成功"
            }
        else:
            return {
                "status": 404,
                "message": "Not Found",
                "data": "没有此用户"
            }
    except ValueError as e:
        return {
            "status": 400,
            "message": str(e),
            "data": None
        }
    except Exception as e:
        return {
            "status": 500,
            "message": f"Error updating user: {str(e)}",
            "data": "更新用户失败"
        }


@users.delete("/delete/{user_id}")
async def delete_user_api(user_id: int):
    """
    删除用户
    """
    try:
        deleted = await delete_user(user_id)
        if deleted:
            return {
                "status": 200,
                "message": "success",
                "data": "删除成功"
            }
        else:
            return {
                "status": 404,
                "message": "Not Found",
                "data": "没有此用户"
            }
    except Exception as e:
        return {
            "status": 500,
            "message": f"Error deleting user: {str(e)}",
            "data": "删除用户失败"
        }
